package cn.edu.sdjzu.xg.kcsj.service;



import cn.edu.sdjzu.xg.kcsj.dao.StudentSignDao;
import cn.edu.sdjzu.xg.kcsj.domain.StudentSign;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class StudentSignService {
    private static  StudentSignService studentSignService = null;
    private static StudentSignDao studentSignDao = StudentSignDao.getInstance ();
    private StudentSignService() {
    }
    public static StudentSignService getInstance() {
        if (studentSignService == null){
            studentSignService = new StudentSignService ();
        }
        return studentSignService;
    }


    public Collection<StudentSign> findAll () throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<StudentSign> studentSigns = null;
        studentSigns = studentSignDao.getInstance().findAll(connection,null,null);
        JdbcHelper.close(connection);
        return studentSigns;
    }

    public StudentSign find (int id_int) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        StudentSign studentSign = studentSignDao.find(id_int,connection);
        JdbcHelper.close(connection);
        return studentSign;
    }

    public boolean add (StudentSign studentSignToAdd) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        return studentSignDao.add (studentSignToAdd,connection);
    }

    public boolean update (StudentSign studentSignToUpdate) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        return studentSignDao.update (studentSignToUpdate,connection);
    }

    public boolean delete (int id) throws SQLException {
        Connection connection = JdbcHelper.getConn ();
        return studentSignDao.delete(id,connection);
    }
    public boolean deletes(List<StudentSign> listToDelete) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted =false;
        try {
            for(StudentSign e:listToDelete) {
                int id= e.getId();
                //删除用户
                deleted = studentSignDao.delete(id, connection);
            }
            //提交事务
            connection.commit();
        }catch (Exception e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted ;
    }
}
